<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="用户手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="性别【0男性，1女性】" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别【0男性，1女性】"></el-input>
    </el-form-item>
<!--    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>-->
    <el-form-item label="微信的id" prop="openid">
      <el-input v-model="dataForm.openid" placeholder="微信的id"></el-input>
    </el-form-item>
    <el-form-item label="出生年份" prop="birthYear">
      <el-input v-model="dataForm.birthYear" placeholder="出生年份"></el-input>
    </el-form-item>
<!--    <el-form-item label="逻辑删除位【0删除，1存在】" prop="showStatus">-->
<!--      <el-input v-model="dataForm.showStatus" placeholder="逻辑删除位【0删除，1存在】"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="修改个人信息时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改个人信息时间"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="headPortrait">
      <el-input v-model="dataForm.headPortrait" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="最后一次登录时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="最后一次登录时间"></el-input>
    </el-form-item>
    <el-form-item label="最后一次登录ip" prop="lastLoginIp">
      <el-input v-model="dataForm.lastLoginIp" placeholder="最后一次登录ip"></el-input>
    </el-form-item>
    <el-form-item label="个人简介" prop="personalIntroduction">
      <el-input v-model="dataForm.personalIntroduction" placeholder="个人简介"></el-input>
    </el-form-item>
    <el-form-item label="个人签名，类似qq个签" prop="motto">
      <el-input v-model="dataForm.motto" placeholder="个人签名，类似qq个签"></el-input>
    </el-form-item>
    </el-form>
<!--    <span slot="footer" class="dialog-footer">-->
<!--      <el-button @click="visible = false">取消</el-button>-->
<!--      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>-->
<!--    </span>-->
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          phone: '',
          username: '',
          gender: '',
          password: '',
          openid: '',
          birthYear: '',
          showStatus: '',
          createTime: '',
          updateTime: '',
          headPortrait: '',
          lastLoginTime: '',
          lastLoginIp: '',
          personalIntroduction: '',
          motto: ''
        },
        dataRule: {
          phone: [
            { required: true, message: '用户手机号不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别【0男性，1女性】不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          openid: [
            { required: true, message: '微信的id不能为空', trigger: 'blur' }
          ],
          birthYear: [
            { required: true, message: '出生年份不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '逻辑删除位【0删除，1存在】不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改个人信息时间不能为空', trigger: 'blur' }
          ],
          headPortrait: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          lastLoginTime: [
            { required: true, message: '最后一次登录时间不能为空', trigger: 'blur' }
          ],
          lastLoginIp: [
            { required: true, message: '最后一次登录ip不能为空', trigger: 'blur' }
          ],
          personalIntroduction: [
            { required: true, message: '个人简介不能为空', trigger: 'blur' }
          ],
          motto: [
            { required: true, message: '个人签名，类似qq个签不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/gwy/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.phone = data.user.phone
                this.dataForm.username = data.user.username
                this.dataForm.gender = data.user.gender
                this.dataForm.password = data.user.password
                this.dataForm.openid = data.user.openid
                this.dataForm.birthYear = data.user.birthYear
                this.dataForm.showStatus = data.user.showStatus
                this.dataForm.createTime = data.user.createTime
                this.dataForm.updateTime = data.user.updateTime
                this.dataForm.headPortrait = data.user.headPortrait
                this.dataForm.lastLoginTime = data.user.lastLoginTime
                this.dataForm.lastLoginIp = data.user.lastLoginIp
                this.dataForm.personalIntroduction = data.user.personalIntroduction
                this.dataForm.motto = data.user.motto
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/gwy/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'phone': this.dataForm.phone,
                'username': this.dataForm.username,
                'gender': this.dataForm.gender,
                'password': this.dataForm.password,
                'openid': this.dataForm.openid,
                'birthYear': this.dataForm.birthYear,
                'showStatus': this.dataForm.showStatus,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'headPortrait': this.dataForm.headPortrait,
                'lastLoginTime': this.dataForm.lastLoginTime,
                'lastLoginIp': this.dataForm.lastLoginIp,
                'personalIntroduction': this.dataForm.personalIntroduction,
                'motto': this.dataForm.motto
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
